<template>
  <el-row>
    <DialogExamine :visible.sync="visible" @refresh="refresh" :item="item" :findState="findState"></DialogExamine>
    <el-col :span="24">
      <el-card class="search-form">
        <el-form ref="searchForm" class="el-form-col el-form-s" :model="search"  label-position="left"  label-width="80px">
          <el-row :gutter="15">
            <el-col :span="6">
              <el-form-item prop="credentialCode" label="申请流水">
                <el-input v-model="search.credentialCode" placeholder="申请流水" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="auditState" label="认证类型">
                <el-select v-model="search.auditState" clearable placeholder="认证类型">
                  <el-option
                    v-for="item in typeArr"
                    :key="item.value"
                    :label="item.text"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item prop="workerName" label="师傅名称">
                <el-input v-model="search.workerName" placeholder="师傅名称" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="2" class="text-center">
              <el-button type="success" :loading="searchLoading" plain @click="searchFunc(search)">搜索</el-button>
            </el-col>
            <el-col :span="2" class="text-center">
              <el-button type="primary" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
            </el-col>
            <el-col :span="2" class="text-center">
              <el-button @click="resetFields" plain>重置</el-button>
            </el-col>

          </el-row>

          <el-collapse-transition>
            <template  v-if="isOpen">
              <el-row :gutter="15">
                <el-col :span="6">
                  <el-form-item prop="workerPhone" label="师傅手机">
                    <el-input v-model="search.workerPhone" placeholder="师傅手机" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item prop="foundStartTime" label="申请时间">
                    <el-date-picker
                      placeholder="开始时间"
                      v-model="search.foundStartTime"
                      type="date"
                      value-format="yyyy-MM-dd"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item prop="foundEndTime" label="申请时间">
                    <el-date-picker
                      placeholder="结束时间"
                      v-model="search.foundEndTime"
                      type="date"
                      value-format="yyyy-MM-dd"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="服务城市" prop="addressArr">
                    <cascade-address :level="2" v-model="search.addressArr" placeholder="服务城市"></cascade-address>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item prop="auditName" label="处理人">
                    <el-input v-model="search.auditName" placeholder="处理人" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item prop="auditStartTime" label="处理时间">
                    <el-date-picker
                      v-model="search.auditStartTime"
                      type="date"
                      placeholder="开始时间"
                      value-format="yyyy-MM-dd"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item prop="auditEndTime" label="处理时间">
                    <el-date-picker
                      v-model="search.auditEndTime"
                      type="date"
                      placeholder="结算时间"
                      value-format="yyyy-MM-dd"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
            </template>
          </el-collapse-transition>

        </el-form>
      </el-card>
    </el-col>
    <el-col :span="24">
      <BasicTable
        ref="BasicTable"
        url="/ms-fahuobao-app/credential/findCredentialList"
        :tabs="tabs"
        :columns="columns">
        <template slot="column">
          <el-table-column fixed="right" width="100px" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="showExamineModal(scope.row)"
              >
                {{ findState ? '详情' : '审核' }}
              </el-button>
            </template>
          </el-table-column>
        </template>
      </BasicTable>
    </el-col>
  </el-row>
</template>
<script>
import {filterDataBaseDictionary, unixTimestampFilter} from '@/filters'
import DialogExamine from './DialogExamine'
import {copy, dotData} from '@/utils'
import {getCredentialType} from '@/utils/selectOptions'
export default {
  name: 'certificationExamine',
  components: {
    DialogExamine
  },
  data() {
    let columns = [
      {
        label: '申请流水',
        data: 'credentialCode'
      },
      {
        label: '申请类型',
        data: 'credentialType',
        render(data) {
          return filterDataBaseDictionary(data)
        }
      },
      {
        label: '师傅名称',
        data: 'workerName'
      },
      {
        label: '手机号',
        data: 'workerPhone'
      },
      {
        label: '服务城市',
        data: 'workerProvince-workerCity',
        render(data, full) {
          return `${full.workerProvince} ${full.workerCity}`
        }
      },
      {
        label: '申请时间',
        data: 'foundtime',
        render(data) {
          return unixTimestampFilter(data)
        }
      }
    ]
    let passColumns = copy(columns, true)
    passColumns.splice(passColumns.length, 0, {
      label: '审核人',
      data: 'auditName'
    }, {
      label: '审核结果',
      data: 'auditState',
      render(data, full) {
        if (full.auditState === '1' || full.auditState === 1) {
          return `审核通过`
        } else if (full.auditState === '2' || full.auditState === 2) {
          return `审核驳回`
        } else if (full.auditState === '0' || full.auditState === 0) {
          return `待审核`
        } else {
          return `未知`
        }
      }
    }, {
      label: '审核时间',
      data: 'auditTime',
      render(data, full) {
        return unixTimestampFilter(data)
      }
    })
    return {
      isOpen: false,
      visible: false,
      searchLoading: false,
      search: {
        credentialCode: '',
        auditState: '',
        workerName: '',
        workerPhone: '',
        foundStartTime: '',
        foundEndTime: '',
        auditStartTime: '',
        auditEndTime: '',
        auditName: '',
        auditTime: '',
        addressArr: []
      },
      findState: 0,
      columns,
      tabs: [
        {
          label: '待审核',
          callback: (params) => { // 此处可以修改请求参数
            params.findState = '0'
            this.findState = 0
          }
        },
        {
          label: '已审核',
          callback: (params) => {
            params.findState = '1'
            this.findState = 1
            // 追加列
          },
          columns: passColumns
        }
      ],
      typeArr: [],
      item: {}
    }
  },
  created() {
    getCredentialType().then(arr => {
      this.typeArr = arr
    })
  },
  methods: {
    // 搜索
    searchFunc(search) {
      const basicTable = this.$refs.BasicTable
      if (basicTable) {
        let params = copy(search)
        params.workerProvinceNo = dotData(params, 'addressArr[0]') || ''
        params.workerCityNo = dotData(params, 'addressArr[1]') || ''
        delete params.addressArr
        this.searchLoading = true
        basicTable.search(params).finally(_ => {
          this.searchLoading = false
        })
      }
    },
    // 重置表单
    resetFields() {
      const searchForm = this.$refs.searchForm
      if (searchForm) {
        searchForm.resetFields()
      }
    },
    // 审核
    showExamineModal(data) {
      this.visible = true
      this.item = data
    },
    // 表格刷新
    refresh() {
      this.searchFunc(this.search)
    }
  }
}
</script>
